package com.basic.business.order.service.impl;

import com.basic.business.order.mapper.TOrdersDetailMapper;
import com.basic.business.order.mapper.TOrdersMergeMapper;
import com.basic.common.annotation.DataScope;
import org.springframework.stereotype.Service;
import com.basic.common.core.service.impl.BaseServiceImpl;
import com.basic.business.order.domain.TOrdersMain;
import com.basic.business.order.service.ITOrdersMainService;
import com.basic.business.order.mapper.TOrdersMainMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务-订单主Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TOrdersMainServiceImpl extends BaseServiceImpl<TOrdersMainMapper,TOrdersMain> implements ITOrdersMainService {

    @Resource
    private TOrdersMainMapper tOrdersMainMapper;

    @Resource
    private TOrdersDetailMapper detailMapper;

    @Resource
    private TOrdersMergeMapper mergeMapper;

    @Override
    public List<TOrdersMain> getList(TOrdersMain po) {
        List<TOrdersMain> list = super.getList(po);
        list.forEach(item -> {
            // 状态是待审核且付款时间超过48小时的打标识
            if (item.getSysStatus().equals("WAIT_AUDIT") && item.getPayTime() < System.currentTimeMillis() - 24 * 60 * 60 * 1000) {
                item.setOverdue(true);
            }
        });
        return getTOrdersMains(list);
    }

    @Override
    public List<TOrdersMain> getUndistributedOrder(TOrdersMain tOrdersMain) {
        List<TOrdersMain> undistributedOrder = tOrdersMainMapper.getUndistributedOrder(tOrdersMain);
        // 加载订单明细中的图片
        undistributedOrder.forEach(item -> {
            // 根据sid查询订单明细中的图片，多个图片地址用逗号隔开
            List<String> pics = detailMapper.getPicsByMainSid(item.getSid());
            item.setDetailPics(String.join(",", pics));
            // 卖家备注
            item.setMergeList(mergeMapper.getMergeByMainSid(item.getSid()));
        });
        return undistributedOrder;
    }

    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<TOrdersMain> getMyTasks(TOrdersMain main) {
        List<TOrdersMain> list = tOrdersMainMapper.getMyTasks(main);
        list.forEach(item -> {
            // 状态是待审核且付款时间超过48小时的打标识
            if (item.getSysStatus().equals("WAIT_AUDIT") && item.getOrderStatus().equals("MANUAL-DESIGN") && item.getPayTime() < System.currentTimeMillis() - 48 * 60 * 60 * 1000) {
                item.setOverdue(true);
            }
        });
        return getTOrdersMains(list);
    }

    @Override
    public int getOverdueCount(TOrdersMain tOrdersMain) {
        List<TOrdersMain> list = tOrdersMainMapper.getMyTasks(tOrdersMain);
        return list.size();
    }

    @Override
    public int getOverdue24Count(TOrdersMain tOrdersMain) {
        List<TOrdersMain> list = tOrdersMainMapper.getList(tOrdersMain);
        return list.size();
    }

    /**
     * 补充订单信息
     */
    private List<TOrdersMain> getTOrdersMains(List<TOrdersMain> list) {
        list.forEach(po -> po.setMergeList(mergeMapper.getMergeByMainSid(po.getSid())));
        return list;
    }


}
